package com.derekandbritt.booST.data;

public class SingleItemCache<T> {

	private T cachedItem;
	private long cacheExpiration;
	private long cacheLifetime;
	
	public SingleItemCache(T item, long lifetime) {
		this.cacheLifetime = lifetime;
		updateCache(item);
	}
	
	public void updateCache(T item) {
		this.cachedItem = item;
		this.cacheExpiration = System.currentTimeMillis() + cacheLifetime;
	}
	
	public T getItem() {
		return isValid() ? cachedItem : null;
	}
	
	public boolean isValid() {
		//TODO pull from preferences
		return (cachedItem != null && cacheExpiration > System.currentTimeMillis());
	}
}
